package com.kerchin.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringChain;

import java.util.List;

/**
 * Created by lijuan on 2016/9/12.
 */
public class GridViewAdapter extends BaseAdapter {
    private List<Model> mData;
    private LayoutInflater inflater;
    private TranslateAnimation taLeft, taRight, taTop, taBlow;
    /**
     * 页数下标,从0开始(当前是第几页)
     */
    private int curIndex;
    /**
     * 每一页显示的个数
     */
    private int pageSize;
    Context context;
    boolean showAnim = false;
    public GridViewAdapter(Context context, List<Model> mData, int curIndex, int pageSize) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.mData = mData;
        this.curIndex = curIndex;
        this.pageSize = pageSize;
        InitAnima();
    }
    public void setShowAnim(boolean show){
        showAnim  =show;
    }
    private void InitAnima() {
        // TODO Auto-generated method stub
        taLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taTop = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taBlow = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        taLeft.setDuration(1000);
        taRight.setDuration(1000);
        taTop.setDuration(1000);
        taBlow.setDuration(1000);
    }
    /**
     * 先判断数据集的大小是否足够显示满本页？mData.size() > (curIndex+1)*pageSize,
     * 如果够，则直接返回每一页显示的最大条目个数pageSize,
     * 如果不够，则有几项返回几,(mData.size() - curIndex * pageSize);(也就是最后一页的时候就显示剩余item)
     */
    @Override
    public int getCount() {
        return mData.size() > (curIndex + 1) * pageSize ? pageSize : (mData.size() - curIndex * pageSize);

    }

    @Override
    public Model getItem(int position) {
        return mData.get(position + curIndex * pageSize);
    }

    @Override
    public long getItemId(int position) {
        return position + curIndex * pageSize;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_gridview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tv = (TextView) convertView.findViewById(R.id.textView);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        /**
         * 在给View绑定显示的数据时，计算正确的position = position + curIndex * pageSize，
         */
        int pos = position + curIndex * pageSize;
        viewHolder.tv.setText(mData.get(pos).getName());
        try {
            if (mData.get(pos).getIconRes()==null){
                Glide.with(context).load(mData.get(pos).getImg_Icon()).error(R.drawable.luyan_icon).into(viewHolder.iv);
            }else {
                Glide.with(context).load(mData.get(pos).getIconRes()).error(R.drawable.luyan_icon).into(viewHolder.iv);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*Random ran = new Random();
        int rand = ran.nextInt(2);
        switch (rand) {
            case 0:
                convertView.startAnimation(taLeft);
                break;
            case 1:
                convertView.startAnimation(taRight);
                break;
            case 2:
                convertView.startAnimation(taTop);
                break;
            case 3:
                convertView.startAnimation(taBlow);
                break;
        }*/

        if (!showAnim){
            return convertView;
        }

        convertView.setVisibility(View.INVISIBLE);
        int count = position/4+1;
        final TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,
                0,
                Animation.RELATIVE_TO_SELF,
                0,
                Animation.RELATIVE_TO_SELF,
                count,
                Animation.RELATIVE_TO_SELF,
                0);
        translateAnimation.setDuration(count* 100);

//      final Animation animation = AnimationUtils.loadAnimation(parent.getContext(), R.anim.slide_in_right);
        final View finalConvertView = convertView;
        finalConvertView.clearAnimation();
        convertView.postDelayed(new Runnable() {
            @Override
            public void run() {
                finalConvertView.setVisibility(View.VISIBLE);
                test(finalConvertView);
                //finalConvertView.startAnimation(translateAnimation);
            }
        }, position * 50);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                finalConvertView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //viewHolder.iv.setImageResource(mData.get(pos).getIconRes());
        return convertView;
    }

    private class ViewHolder {
        TextView tv;
        ImageView iv;
    }

    private void test(final View view){
        SpringChain springChain = SpringChain.create(40,6,50,7);

        springChain.addSpring(new SimpleSpringListener() {
            @Override
            public void onSpringUpdate(Spring spring) {
                view.setTranslationY((float) spring.getCurrentValue());
            }
        });

        List<Spring> springs = springChain.getAllSprings();
        for (int i = 0; i < springs.size(); i++) {
            springs.get(i).setCurrentValue(400);
        }

       springChain.setControlSpringIndex(0).getControlSpring().setEndValue(0);
    }
}